import { ActionContext, Payload } from "vuex";

type State = {
  profile: Profile;
};
interface Profile {
  id: string;
  avatar: string;
  nickname: string;
  account: string;
  mobile: string;
  token: string;
}
// 用户信息模块(局部模块)
export default {
  namespaced: true, // 开启命名空间
  state: {
    profile: {
      id: "",
      avatar: "",
      nickname: "yee",
      account: "",
      mobile: "",
      token: "",
    },
  },
  mutations: {
    // 定义mutations，用于同步修改状态
    updateNickname(state: State, payload: string) {
      state.profile.nickname = payload;
    },
  },
  actions: {
    // 定义actions，用于异步修改状态
    // 2秒后更新状态
    updateNickname(context: ActionContext<object, object>, payload: Payload) {
      setTimeout(() => {
        context.commit("updateNickname", payload);
      }, 2000);
    },
  },
  getters: {
    // 定义一个getters
    formatNickname(state: State) {
      return "Hi " + state.profile.nickname;
    },
  },
};
